package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.pojo.Drugs;
import com.inspur.his.pojo.Drugsstorrecord;
import com.inspur.his.service.DrugsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @Date: 2022/12/20 16:00
 */
@Controller
public class DrugsController {
    @Autowired
    DrugsService drugsService;

    @RequestMapping("drugsListController")
    public String drugsList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) throws UnsupportedEncodingException{
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取页面提交来的查询条件，来自前端页面查询部分标签的 name属性
        String DRUGSID = request.getParameter("mediNo");//
        String NAME = request.getParameter("mediName");//
        System.out.println("药品ID："+DRUGSID);
        System.out.println("药品名称："+NAME);
        HashMap map = new HashMap();
        map.put("DRUGSID",String.valueOf(DRUGSID));
        map.put("NAME",String.valueOf(NAME));
        PageHelper.startPage(pn, 5);
        List<Drugs> drugsList = drugsService.drugsList(map);
        /*这里pageInfo.list就能获得分页完的数据*/
        PageInfo<Drugs> pageInfo = new PageInfo<Drugs>(drugsList,drugsList.size());
        /*Model主要用于传值给jsp*/
        model.addAttribute("drugsList",drugsList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("vmediNo",DRUGSID);
        model.addAttribute("vmediName",NAME);
        HttpSession s = request.getSession(true);
        s.setAttribute("drugsList",drugsList);
        s.setAttribute("pageInfo",pageInfo);
        s.setAttribute("vmediNo",DRUGSID);
        s.setAttribute("vmediName",NAME);

        return "药品信息管理/药品列表.jsp";
    }

    @RequestMapping("drugsFromController")
    public String DrugsFrom(HttpServletRequest request) throws UnsupportedEncodingException{
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //跳转到添加页面
        return "药品信息管理/药品添加.jsp";
    }

    @RequestMapping("drugsAddController")
    public String drugsAdd(HttpServletRequest request) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //2.接受页面提交的数据
        String DRUGSID = request.getParameter("drugsId");
        String NAME = request.getParameter("name");
        String TYPE = request.getParameter("type");
        double PRICE = Double.parseDouble(request.getParameter("price"));
        String PRODUCTDATE = request.getParameter("productDate");
        String SUPPLYUNIT = request.getParameter("supplyUnit");
        int NUM = Integer.parseInt(request.getParameter("num"));
        Drugs drugs = new Drugs();
        drugs.setDRUGSID(DRUGSID);
        drugs.setNAME(NAME);
        drugs.setPRICE(PRICE);
        drugs.setTYPE(TYPE);
        drugs.setPRODUCTDATE(PRODUCTDATE);
        drugs.setSUPPLYUNIT(SUPPLYUNIT);
        drugs.setNUM(NUM);

        drugsService.drugsAdd(drugs);
        return "redirect:drugsListController";
    }

    @RequestMapping("drugsDeleteController")
    public String drugsDelete(HttpServletRequest request) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        String DRUGSID = request.getParameter("drugsId");
        Drugs drugs = new Drugs();
        drugs.setDRUGSID(String.valueOf(DRUGSID));
        Drugs drugs2 = drugsService.findById(drugs);
        drugsService.drugsDelete(drugs2);
        return "redirect:drugsListController";
    }
}
